package com.ywu.application.standard.dataSource.source.field.impl;

import com.ywu.application.standard.dataSource.dto.YwuDataTableFieldDto;
import com.ywu.application.standard.dataSource.entity.YwuDataTableEntity;
import com.ywu.application.standard.dataSource.source.field.YwuDataSourceFieldSqlBuild;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @ClassName YwuBuildFieldTypeSql
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/10/28 20:17
 * @Version 1.0
 **/
@Component
@Order(2)
@Slf4j
public class YwuBuildFieldTypeSql implements YwuDataSourceFieldSqlBuild {

    @Override
    public String buildSql(YwuDataTableEntity ywuDataTableEntity, YwuDataTableFieldDto ywuDataTableFieldDto) {
        String res = "";
        if ("varchar".equals(ywuDataTableFieldDto.getFieldType())) {
            res = String.format("%s(%s) ", ywuDataTableFieldDto.getFieldType(), ywuDataTableFieldDto.getFieldLength());
        } else if ("enum".equals(ywuDataTableFieldDto.getFieldType())) {
            String[] split = ywuDataTableFieldDto.getEnumInfo().split(",");
            StringBuilder enumStr = new StringBuilder();
            for (String s : split) {
                enumStr.append("'").append(s).append("', ");
            }
            String substring = enumStr.substring(0, enumStr.length() - 2);
            res = String.format("%s(%s) ", ywuDataTableFieldDto.getFieldType(), substring);
        } else {
            res = String.format("%s ", ywuDataTableFieldDto.getFieldType());
        }
        return res;
    }
}
